package com.example.ymm.modules1.service.impl.user;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.exception.YmException;
import com.example.ymm.model.TableList;
import com.example.ymm.model.YmPageDto;
import com.example.ymm.modules1.entity.my.user.OfficePostRequest;
import com.example.ymm.modules1.mapper.my.user.OfficePostRequestMapper;
import com.example.ymm.modules1.query.OfficePostRequestQueryCriteria;
import com.example.ymm.modules1.service.dto.ImportPostRequestDto;
import com.example.ymm.modules1.service.user.OfficePostRequestService;
import com.example.ymm.util.QueryHelpPlus;
import com.example.ymm.util.UtilValidate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
@Slf4j
public class OfficePostRequestServiceImpl extends BaseServiceImpl<OfficePostRequestMapper, OfficePostRequest> implements OfficePostRequestService {



    @Override
    public TableList<OfficePostRequest> list(OfficePostRequestQueryCriteria criteria, YmPageDto pageDto) {
        IPage<OfficePostRequest> page = new Page<OfficePostRequest>(pageDto.getPage(),pageDto.getPageSize());
        baseMapper.selectPage(page, QueryHelpPlus.getPredicate(OfficePostRequest.class, criteria));
        TableList<OfficePostRequest> tableList=new TableList(page);
        return tableList;
    }

    public List<OfficePostRequest> queryAll(OfficePostRequestQueryCriteria criteria) {
        QueryWrapper predicate = QueryHelpPlus.getPredicate(OfficePostRequest.class, criteria);
        predicate.orderByDesc("create_time");
        List<OfficePostRequest> result =baseMapper.selectList(predicate);
        for (OfficePostRequest officePostRequest : result) {
            String value=null;
            if(UtilValidate.isNotEmpty(officePostRequest.getEducation())){
                value=officePostRequest.getRoleName()+":"+officePostRequest.getEducation();
            }else{
                value=officePostRequest.getRoleName();
            }
            officePostRequest.setValue(value);
        }

        return result;
    }


    @Override
    public OfficePostRequest findByRoleName(String roleName) {
        OfficePostRequestQueryCriteria c=new OfficePostRequestQueryCriteria();
        c.setRoleName(roleName);
        List<OfficePostRequest> list = queryAll(c);
        if(list.size()>0){
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<String> postRequestImport(ImportPostRequestDto dto) {
        List<String> erros=new ArrayList<>();
        List<OfficePostRequest> list = dto.getList();
        if(UtilValidate.isEmpty(list)){
            throw new YmException("导入数据不能为空");
        }
        for(OfficePostRequest postRequest:list){
            if(UtilValidate.isNotEmpty(postRequest.getDeptName())){
                postRequest.setDeptName(postRequest.getDeptName().trim());
            }
            if(UtilValidate.isNotEmpty(postRequest.getRoleName())){
                postRequest.setRoleName(postRequest.getRoleName().trim());
                OfficePostRequest officePostRequest = findByRoleName(postRequest.getRoleName());
                if(UtilValidate.isEmpty(officePostRequest)){
                    saveOrUpdate(postRequest);
                }
            }
        }
        return erros;
    }
}
